namespace my_stl{
	class string{
	public:
		// 迭代器（原生指针）
		typedef char* iterator;
		typedef char* reverse_iterator;

		/*  Member functions  */
		// 构造函数
		string(const char* str = ""){
			if (str == NULL) assert(false);
			_size = strlen(str);
			_str = new char[_size + 1];  //+1加上字符串结尾的'/0'
			strcpy(_str, str);
			_capacity = _size;
		}
		string(size_t n, char ch){
			_str = new char[n + 1];
			_str[n] = '\0';
			_size = _capacity = n;
		}
		// 拷贝构造
		string(const string& s) :_str(NULL){
			string temp(s._str);
			this->swap(temp);
		}
		// 赋值运算符重载
		string& operator=(string s){
			swap(s); // 传参时拷贝构造临时对象
			return *this;
		}

		~string(){
			if (_str != NULL){
				delete[] _str;
				_str = NULL;
				_size = 0;
				_capacity = 0;
			}
		}

		/* Iterators: */
		iterator begin(){ return _str; }
        iterator end(){ return _str + _size; }

		reverse_iterator rbegin(){	return end(); }
		reverse_iterator rend(){ return begin(); } 

		/* Capacity: */
		size_t size()const{ return _size;	}
        size_t length()const{ return _size; }
    	size_t capacity()const{ return _capacity; }
		bool empty()const{ return _size == 0; }
        void clear(){
			_str[0] = '\0';
			_size = 0;
			return;
		}

		void resize(size_t newsize, char ch){
			size_t oldsize = _size;
			if (oldsize > newsize) _str[newsize] = '\0';
			else{
				if (newsize > _capacity){
					// 扩容
					reserve(2 * _capacity);  // 以2倍方式进行扩容
					append(newsize - oldsize, ch);
				}
				_size = newsize;
			}
		}
		void resize(size_t  newsize){
			resize(newsize, char());
		}

		void reserve(size_t newcapacity){
			size_t oldcapacity = _capacity;
			if (oldcapacity < newcapacity){
				char* temp = new char[newcapacity + 1];
				strcpy(temp, _str);
				delete[] _str;
				_str = temp;
				_capacity = newcapacity;
			}
		}

		/* Element access: */
		char& operator[](size_t index){
			assert(index < _size);
			return _str[index];
		}
		const char& operator[](size_t index)const{
			assert(index < _size);
			return _str[index];
		}

		char& back(){ return _str[_size - 1]; }
		const char& back() const{ return _str[_size - 1]; }
		char& front(){ return _str[0]; }
		const char& front() const{ return _str[0]; }

		/* Modifiers: */
		string& operator+=(char ch){
			push_back(ch);
			return *this;
		}
		string& operator+=(const char* str){
			append(str);
			return *this;
		}
		string& operator+=(const string& s){
			append(s.c_str());
			return *this;
		}

		string& append(size_t n, char ch){
			if (n + _size > _capacity)
				reserve(n + _size);
			memset(_str + _size, ch, n);
			_size += n;
			_str[_size] = '\0';
			return *this;
		}
		string& append(const char* str){
			size_t size = strlen(str);
			if (size + _size > _capacity)
				reserve(size + _size);
			strcat(_str, str);
			_size += size;
			return *this;
		}
		void push_back(char ch){ append(1, ch); }
		string& insert(size_t pos, const string& s){
			if (_size + s._size > _capacity){
				reserve(_size + s._size);
			}
			// 搬移元素
			for (int i = _size; i >= (int)pos; --i){
				_str[i+s._size] = _str[i];
			}
			strncpy(_str + pos, s._str, s._size);
			_size += s._size;
			return *this;
		}

		string& erase(size_t pos = 0, size_t n = npos){
			if (pos == npos || pos + n >= _size){
				_str[pos] = '\0';
				_size = pos;
			}
			else{
				_str[pos] = '\0';
				strcat(_str + pos, _str + pos + n);
			}
		}

		/* String operations: */
		const char* c_str()const{
			return _str;
		}

		size_t find(char ch, size_t pos = 0){
			for (size_t i = 0; i < _size; ++i){
				if (ch == _str[i]) return i;
			}
			return npos;
		}

		size_t rfind(char ch, size_t pos = npos){
			if (pos == npos) pos = _size - 1;
			for (size_t i = pos; i >= 0; --i){
				if (ch == _str[i]) return i;
			}
			return npos;
		}
		string substr(size_t pos = 0, size_t n = npos){
			if (n == npos) n = _size - pos;
			char* temp = new char[n + 1];
			strncpy(temp, _str + pos, n);
			temp[n] = '\0';
			return temp;
		}
		int compare(const string& s){ strcmp(_str,s._str); }
		int compare(const char* s){ strcmp(_str, s); }


		/* Non-member function overloads */
		void swap(string&s){
			std::swap(_str, s._str);
			std::swap(_size, s._size);
			std::swap(_capacity, s._capacity);
		}
		   // 重载输入输出流运算符时会报参数过多，所以重载成友元或是全局函数
		friend std::istream& operator>>(std::istream& _cin, string& s){
			s.clear();
			char ch = _cin.get();
			while (ch != '\n' || ch != ' '){
				s += ch;
				ch = _cin.get();
			}
			return _cin;
		}

		friend std::ostream& operator<<(std::ostream& _cout, const string& s){
			_cout << s._str;
			return _cout;
		}
	private:
		char* _str;
		size_t _size;     // 有效元素
		size_t _capacity; // 容量

		/*   vs下维护了长度16的数组，如果长度较小直接使用数组，长度超过在扩容；
		vs STL与Linux下SGI STL扩容方式不一样     */

		static size_t npos;
	};
	size_t string::npos = -1;
}